﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSWXBLPDA
{
    public partial class SCAN : Form
    {
        private LoginReference.Registration Reg = new BSWXBLPDA.LoginReference.Registration();
        private string ID;
        private string name;

        public SCAN()
        {
            InitializeComponent();
        }

        public SCAN(string _ID,string _name)
        {
            InitializeComponent();
            this.ID = _ID;
            this.name = _name;
            tb_FCARD.Focus();
           
        }


        //登记不良
        private void button1_Click(object sender, EventArgs e)
        {
            string FCARD = string.Empty;
            string CREATOR = string.Empty;
            string CREATETIME = string.Empty;
            string ls_Weight =  string.Empty;
            float lf_Weight = 0;
            string Reason = string.Empty;
            bool result = false;


            FCARD = tb_FCARD.Text.Trim();
            if (FCARD == null || FCARD == "")
            {
                MessageBox.Show("请扫描F卡", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
               
            }

             ls_Weight = tb_Weight.Text.Trim();
             if (ls_Weight == null || ls_Weight == "")
             {
                MessageBox.Show("请填写重量", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
               
             }
             else 
             {               
                 lf_Weight = float.Parse(tb_Weight.Text.ToString());               
             }

             Reason = tb_Reason.Text.Trim();
             if (Reason == null || Reason == "")
             {
                 MessageBox.Show("请填写原因", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
             }

             CREATETIME = tb_CREATETIME.Text;

             result = Reg.Registrate(FCARD, lf_Weight, Reason, this.name, CREATETIME);
             if (result == true)
             {
                 tb_FCARD.Text ="";
                 tb_CREATOR.Text = "";
                 tb_CREATETIME.Text = "";
                 tb_Weight.Text = "";
                 tb_Reason.Text = "";

                 tb_FCARD.Focus();
             }
             else
             {
                 MessageBox.Show("登记不良失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
             }
        }


        //校验校验字符只能输入小数和数字
        //public bool JudgeWeight(string _Weight)
        //{
        //    bool result = true;
        //    float Weight = 0;
        //    int i = _Weight.IndexOf(",");

        //    if (i == -1)
        //    {
        //        try
        //        {
        //            Weight = float.Parse(tb_Weight.Text.ToString().Trim());
        //        }
        //        catch (Exception ex)
        //        {
        //            tb_Weight.Text = ex.ToString();
        //            result = false;

        //        }
             
        //    }
        //    else 
        //    {
        //        result = false;
        //    }
           
        //    return result;
        //}

        //校验字符只能输入小数和数字
        private void tb_Weight_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != (char)('.'))
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_Weight.Text = "";
                e.Handled = true;
            }
           


            /*小数点只能输入一次*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_Weight.Text = "";
                e.Handled = true;
            }
            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_Weight.Text = "";
                e.Handled = true;
            }

            /*第一位是0，第二位必须为小数点*/
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                MessageBox.Show("请输入正确的数字");
                this.tb_Weight.Text = "";
                e.Handled = true;
            }
        }
       
       
        //获取F卡码
        private void tb_FCARD_TextChanged(object sender, EventArgs e)
        {
            string FCARD = tb_FCARD.Text.ToString().Trim();
            if (tb_FCARD.Text.Trim().Length == 26)
            {                
                tb_CREATOR.Text = this.name;
                tb_CREATETIME.Text = DateTime.Now.ToString() ;
                tb_Weight.Text = "";
                tb_Reason.Text = "";
                tb_Weight.Focus();               
            }         
        }

        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            Main iw = new Main(this.ID, this.name);
            iw.Show();
            this.Hide();
        }


        private void tb_Weight_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tb_Reason.Focus();
                //string ls_Weight = tb_Weight.Text.Trim();

                //if (ls_Weight == null || ls_Weight == "")
                //{
                //    MessageBox.Show("请填写重量", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                //}
                //else
                //{
                //    if (!JudgeWeight(ls_Weight))
                //    {
                //        MessageBox.Show("输入错误，请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

                //    }

                //}
            }
        }

       
      
        

       
    }
}